﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ocmf.Types;

namespace Ocmf.Forms
{
    public partial class EditScreenTemplateForm : Form
    {
        public ScreenTemplate ScreenTemplate { get; set; }
        
        public EditScreenTemplateForm(ScreenTemplate screenTemplate)
        {
            InitializeComponent();

            ScreenTemplate = screenTemplate;
            gridSizeToolStripComboBox.SelectedIndex = 1;

            //screenPanel.BackColor = backgroundColourToolStripButton.BackColor = ScreenTemplate.BackgroundColour;
            aspectRationToolStripComboBox.SelectedItem = ScreenTemplate.AspectRatio;
            gridSizeToolStripComboBox.SelectedItem = ScreenTemplate.GridSize;
        }

        private void addPlaceholderToolStripButton_Click(object sender, EventArgs e)
        {
            var placeholder = new ImagePlaceholder() { Size = new Size(100, 100), Location = new Point(0, 0), BorderStyle = BorderStyle.FixedSingle};
            screenPanel.Controls.Add(placeholder);
        }

        private void backgroundColourToolStripButton_Click(object sender, EventArgs e)
        {
            var colorDialog = new ColorDialog() { Color = backgroundColourToolStripButton.BackColor };
            if (colorDialog.ShowDialog() == DialogResult.OK)
                screenPanel.BackColor = backgroundColourToolStripButton.BackColor = colorDialog.Color;
        }

        private void aspectRationToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            var maxWidth = backgroundPanel.Width - 20;
            var maxHeight = backgroundPanel.Height - 90;
            var aspectRatio = new SizeF(float.Parse(aspectRationToolStripComboBox.SelectedItem.ToString().Split(':')[0]), float.Parse(aspectRationToolStripComboBox.SelectedItem.ToString().Split(':')[1]));

            screenPanel.Size = (maxWidth / aspectRatio.Width) * aspectRatio.Height > maxHeight ? new Size((int)((maxHeight / aspectRatio.Height) * aspectRatio.Width), maxHeight) : new Size(maxWidth, (int)((maxWidth / aspectRatio.Width) * aspectRatio.Height));

            screenPanel.Top = (backgroundPanel.Height - screenPanel.Height) / 2;
            screenPanel.Left = (backgroundPanel.Width - screenPanel.Width) / 2;
        }

        private void gridSizeToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            screenPanel.GridSize = int.Parse(gridSizeToolStripComboBox.SelectedItem.ToString().Replace(" px", ""));

            screenPanel.Invalidate();
        }

        private void saveAsToolStripButton_Click(object sender, EventArgs e)
        {
            var saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "Save Screen Template...";
            saveFileDialog.Filter = "Screen Template File (*" + LocalSettings.FileExtensionScreenTemplate + ")|*" + LocalSettings.FileExtensionScreenTemplate;
            saveFileDialog.InitialDirectory = LocalSettings.ApplicationScreenTemplateFolder;

            if(saveFileDialog.ShowDialog() == DialogResult.OK)
                screenPanel.Save(saveFileDialog.FileName);
        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            //ensure the most up to date screen templates are available
            if (LocalSettings.DbOnline) DatabaseProvider.RefreshScreenTemplates();

            var openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "Open Screen Template...";
            openFileDialog.Filter = "Screen Template File (*" + LocalSettings.FileExtensionScreenTemplate + ")|*" + LocalSettings.FileExtensionScreenTemplate;
            openFileDialog.InitialDirectory = LocalSettings.ApplicationScreenTemplateFolder;

            if(openFileDialog.ShowDialog() == DialogResult.OK)
                    screenPanel.Open(openFileDialog.FileName);
        }

        private void deletePlaceholderToolStripButton_Click(object sender, EventArgs e)
        {
            screenPanel.Controls.RemoveAt(screenPanel.Controls.Count - 1);
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            if (screenPanel.FileName == null)
                saveAsToolStripButton_Click(this, EventArgs.Empty);
            else
                screenPanel.Save();
        }
    }
}
